//
//  OtherUsersDao.m
//  极致荟
//
//  Created by hetao on 14-8-18.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "OtherUsersDao.h"
#import "FMDatabase.h"
#import "VXSqli.h"

@implementation OtherUsersDao

///添加好友
+ (void)insertData:(OtherUsers *)ouser
{
    FMDatabase *DB=[VXSqli shareVxsqlit].db;
    FMResultSet *set= [DB executeQuery:@"select * from Friend where MyId=? and FriendId=?",[NSNumber numberWithInt:[User defuceUser].userId],[NSNumber numberWithInt:ouser.userId]];
    if(!set.next)
    {
        if([DB executeUpdate:@"insert into Friend(MyId,FriendId,FriendHead,FriendName,BName,XB) values(?,?,?,?,?,?)",[NSNumber numberWithInt:[User defuceUser].userId],[NSNumber numberWithInt:ouser.userId],ouser.userHeadImage,ouser.name,ouser.bname,[NSNumber numberWithInt:0]])
        {
            NSLog(@"好友添加成功");
        }else
        {
            
            NSLog(@"好友添加失败");
        }

    }else
    {
        [self updata:ouser];
    }
}

///查询全部好友
+ (NSMutableArray *) selectData
{
     FMDatabase *DB=[VXSqli shareVxsqlit].db;
    FMResultSet *set=[DB executeQuery:@"select * from Friend where MyId=?",[NSNumber numberWithInt:[User defuceUser].userId]];
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    while (set.next)
    {
        OtherUsers *ouser=[[OtherUsers alloc] init];
        ouser.userId=[set intForColumn:@"FriendId"];
        ouser.name=[set stringForColumn:@"FriendName"];
        ouser.userHeadImage=[set stringForColumn:@"FriendHead"];
        ouser.bname=[set stringForColumn:@"BName"];
        ouser.isAs=[set boolForColumn:@"XB"];
        [arr addObject:ouser];
    }
    [set close];
    return arr;
}

///查询好友
+ (NSMutableArray *)selectUserName:(NSString *)name
{
    FMDatabase *DB=[VXSqli shareVxsqlit].db;
    NSString *str=@"'%";
    str=[str stringByAppendingString:name];
    str=[str stringByAppendingString:@"%'"];

    NSString *spl=[NSString stringWithFormat:@"select * from Friend where FriendName like %@ or BName like %@ ",str,str];
    FMResultSet *set=[DB executeQuery:spl];
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    while (set.next)
    {
        
        OtherUsers *ouser=[[OtherUsers alloc] init];
        ouser.userId=[set intForColumn:@"FriendId"];
        ouser.name=[set stringForColumn:@"FriendName"];
        ouser.userHeadImage=[set stringForColumn:@"FriendHead"];
        ouser.bname=[set stringForColumn:@"BName"];
        ouser.isAs=[set boolForColumn:@"XB"];
        if([User defuceUser].userId!=ouser.userId)
        {
            [arr addObject:ouser];
        }
        
    }
    [set close];
    return arr;
}
///查询最近聊天
+ (NSMutableArray *)selectNoteMessage
{
    
    FMDatabase *DB=[VXSqli shareVxsqlit].db;
    FMResultSet *set=[DB executeQuery:@"select * from Friend where MyId=? and Content!='' order by NoteDate desc",[NSNumber numberWithInt:[User defuceUser].userId]];
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    while (set.next)
    {
        OtherUsers *ouser=[[OtherUsers alloc] init];
        Note *note=[[Note alloc] init];
        ouser.nubmer=[set intForColumn:@"Nuber"];
        ouser.userId=[set intForColumn:@"FriendId"];
        ouser.name=[set stringForColumn:@"FriendName"];
        ouser.userHeadImage=[set stringForColumn:@"FriendHead"];
        ouser.bname=[set stringForColumn:@"BName"];
        ouser.isAs=[set boolForColumn:@"XB"];
        note.content=[set stringForColumn:@"Content"];
        note.noteType=[set intForColumn:@"NoteTyoe"];
        
        ouser.note=note;
        
        [arr addObject:ouser];
    }
    [set close];
    return arr;
}
///删除好友
+ (void)dorpData:(OtherUsers *)ouser
{
     FMDatabase *DB=[VXSqli shareVxsqlit].db;
    if(ouser)
    {
        [DB executeUpdate:@"delete from Friend where FriendId=?",[NSNumber numberWithInt:ouser.userId]];
    }else
    {
        [DB executeUpdate:@"delete from Friend"];
    }
}

///修改好友信息
+ (void)updata:(OtherUsers *)ouser
{
   FMDatabase *DB=[VXSqli shareVxsqlit].db;
    [DB executeUpdate:@"update Friend set FriendHead=?,FriendName=?,BName=?,XB=? where FriendId=? and MyId=?",ouser.userHeadImage,ouser.name,ouser.bname,[NSNumber numberWithBool:ouser.isAs],[NSNumber numberWithInt:ouser.userId],[NSNumber numberWithInt:[User defuceUser].userId]];
}

///修改最近聊天数据
+ (void)NoteMessage:(OtherUsers *)ouser
{
    FMDatabase *DB=[VXSqli shareVxsqlit].db;
    [DB executeUpdate:@"update Friend set Content=?,NoteDate=?,NoteTyoe=?  where FriendId=? and MyId=?",ouser.note.content,[NSNumber numberWithLongLong:[ouser.note.noteDate timeIntervalSince1970]],[NSNumber numberWithInt:ouser.note.noteType],[NSNumber numberWithInt:ouser.userId],[NSNumber numberWithInt:[User defuceUser].userId]];
}

///修改未查看的聊天记录数量
+ (void)upNuber:(OtherUsers *)ouser
{
    FMDatabase *DB=[VXSqli shareVxsqlit].db;
    FMResultSet *set=[DB executeQuery:@"select * from Friend where FriendId=? and MyId=?",[NSNumber numberWithInt:ouser.userId],[NSNumber numberWithInt:[User defuceUser].userId]];
    
    while (set.next)
    {
        int nuber=[set intForColumn:@"Nuber"];
        nuber++;
        [DB executeUpdate:@"update Friend set Nuber=? where FriendId=? and MyId=?",[NSNumber numberWithInt:nuber],[NSNumber numberWithInt:ouser.userId],[NSNumber numberWithInt:[User defuceUser].userId]];
    }
}

///清空未查看的聊天记录数量
+ (void)dorpNuber:(OtherUsers *)ouser
{
    FMDatabase *DB=[VXSqli shareVxsqlit].db;
    [DB executeUpdate:@"update Friend set Nuber=0 where FriendId=? and MyId=?",[NSNumber numberWithInt:ouser.userId],[NSNumber numberWithInt:[User defuceUser].userId]];
}
@end
